package sf.common.wrapper;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Page<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 总数
     */
    private long totalCount;
    /**
     * 总页数
     */
    private int totalPage;

    /**
     * 第几页
     */
    private int pageNumber;
    /**
     * 每页大小
     */
    private int pageSize;

    /**
     * 起始页码
     */
    private long startRow;
    /**
     * 末行
     */
    private long endRow;
    /**
     * 数据
     */
    private List<T> list;

    public Page() {
        this(0, 10, 0);
    }

    /**
     * 构造
     * @param total 全部记录的总数
     */
    public Page(int total) {
        this(0, 10, total);
    }

    /**
     * 构造
     * @param startRow 起始行
     * @param pageSize 每页大小
     * @param total    全部记录的总数
     */
    public Page(long startRow, int pageSize, long total) {
        this.totalCount = total;
        this.pageSize = pageSize;
        this.startRow = startRow;
        this.totalPage = calcTotalPage(pageSize, totalCount);
        this.pageNumber = calcPageNumber(pageSize, startRow);
    }

    public Page(int pageNum, int pageSize) {
        this((long) (pageNum - 1) * pageSize, pageSize, 0);
        this.pageNumber = pageNum;
    }

    /**
     * 构造
     * @param total 记录总数
     * @param data  当前页数据
     */
    public Page(long startRow, int pageSize, int total, List<T> data) {
        this.totalCount = total;
        this.startRow = startRow;
        this.pageSize = pageSize;
        this.list = data;
        this.totalPage = calcTotalPage(pageSize, totalCount);
        this.pageNumber = calcPageNumber(pageSize, startRow);
    }

    /**
     * 计算总页码。
     */
    public static int calcTotalPage(int pageSize, long totalCount) {
        int totalPage = 0;
        if (pageSize > 0 && totalCount > 0) {
            totalPage = (int) (((totalCount - 1) / pageSize) + 1);
        }
        return totalPage;
    }

    public static int calcPageNumber(int pageSize, long startRow) {
        int pageNumber = 0;
        if (pageSize <= 0 || startRow <= 0) {
            pageNumber = 1;
        } else {
            pageNumber = (int) (startRow / pageSize + 1);
        }
        return pageNumber;
    }

    public static long calcStartRow(int pageNumber, int pageSize) {
        long startRow = 0;
        if (pageSize > 0 && pageNumber > 1) {
            startRow = (long) (pageNumber - 1) * pageSize;
        }
        return startRow;
    }

    /**
     * 返回记录数据
     * @return
     */
    public List<T> getList() {
        if (list == null) {
            return Collections.emptyList();
        }
        return list;
    }

    /**
     * 设置记录数据
     * @param list
     */
    public Page<T> setList(List<T> list) {
        this.list = list;
        return this;
    }

    /**
     * 设置记录数据
     * @param list
     */
    public Page<T> setList(T[] list) {
        this.list = Arrays.asList(list);
        return this;
    }

    /**
     * 获得每页的记录数
     * @return
     */
    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }


    public int getPageNumber() {
        return pageNumber;
    }

    public void setPageNumber(int pageNumber) {
        this.pageNumber = pageNumber;
    }

    public long getStartRow() {
        return startRow;
    }

    public void setStartRow(long startRow) {
        this.startRow = startRow;
    }

    public long getEndRow() {
        return endRow;
    }

    public void setEndRow(long endRow) {
        this.endRow = endRow;
    }

    /**
     * 返回记录总数
     * @return
     */
    public long getTotalCount() {
        return totalCount;
    }

    /**
     * 设置记录总数
     * @param totalCount
     */
    public void setTotalCount(long totalCount) {
        this.totalCount = totalCount;
    }

    /**
     * 获得总页数
     * @return
     */
    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    @Override
    public String toString() {
        return "Page{" +
                "totalCount=" + totalCount +
                ", pageNumber=" + pageNumber +
                ", pageSize=" + pageSize +
                ", startRow=" + startRow +
                ", endRow=" + endRow +
                ", totalPage=" + totalPage +
                ", list=" + list +
                '}';
    }
}